/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	IndexSource.cpp
*
*	Comments	-	See IndexSource.h
*
**************************************************************************************/
#include "../Include/IndexSource.h"

namespace Pulse
{
	IndexSource::IndexSource( void )
	{
		m_numIndices = 0;
		m_bUse16Bit = TRUE;
		//m_writeMarker = 0;
		m_pBuffer = PSX_NULL;
	}

	IndexSource::IndexSource( SIZE_T32 count, BOOL bUse16Bit )
	{
		m_numIndices = count;
		m_bUse16Bit = bUse16Bit;
		//m_writeMarker = 0;
		m_pBuffer = PSX_NULL;

		ResizeBuffer( count );
	}

	EErrorCode::Type IndexSource::Initialize( SIZE_T32 count, BOOL bUse16Bit )
	{
		m_numIndices = count;
		m_bUse16Bit = bUse16Bit;
		//m_writeMarker = 0;
		m_pBuffer = PSX_NULL;

		return ResizeBuffer( count );
	}

	IndexSource::~IndexSource( void )
	{
		ClearBuffer();
	}

	/*UPOS_T32 IndexSource::AddIndex( UINT16 index )
	{
		if ( CheckCanAdd( TRUE ) )
		{
			m_pIndices16[m_writeMarker] = index;
			++m_writeMarker;
		}
		
		return m_writeMarker;
	}

	UPOS_T32 IndexSource::AddIndex( UINT32 index )
	{
		if ( CheckCanAdd( FALSE ) )
		{
			m_pIndices32[m_writeMarker] = index;
			++m_writeMarker;
		}
		
		return m_writeMarker;
	}

	UPOS_T32 IndexSource::AddIndices( SIZE_T32 numIndices, UINT16 *pIndices )
	{
		if ( CheckCanAdd( TRUE, numIndices ) )
		{
			PSX_MemCopyPerByte( &m_pIndices16[m_writeMarker], pIndices, sizeof(UINT16) * numIndices );
			m_writeMarker += numIndices;
		}

		return m_writeMarker;
	}

	UPOS_T32 IndexSource::AddIndices( SIZE_T32 numIndices, UINT32 *pIndices )
	{
		if ( CheckCanAdd( TRUE, numIndices ) )
		{
			PSX_MemCopyPerByte( &m_pIndices32[m_writeMarker], pIndices, sizeof(UINT32) * numIndices );
			m_writeMarker += numIndices;
		}

		return m_writeMarker;
	}*/

	EErrorCode::Type IndexSource::ResizeBuffer( SIZE_T32 count )
	{
		ClearBuffer();

		Set16BitUsage( PSX_USHORT_MAX ? TRUE : FALSE );

		BYTE *pNewBuffer;
		SIZE_T elementSize;

		if ( Is16BitIndices() )
			elementSize = sizeof( UINT16 );
		else
			elementSize = sizeof( UINT32 );

		pNewBuffer = new BYTE[elementSize * count];

		if ( pNewBuffer == PSX_NULL )
			return EErrorCode::MEMORY;

		//if ( m_writeMarker )
		//{
		//	UINT copyCount = m_writeMarker > count ? count : m_writeMarker;
		//	PSX_MemCopyPerByte( pNewBuffer, m_pBuffer, elementSize * copyCount );
		//	m_writeMarker = copyCount;
		//}

		ClearBuffer();
		m_pBuffer = pNewBuffer;
		m_numIndices = count;

		return EErrorCode::OKAY;
	}

	void IndexSource::ClearBuffer( void )
	{
		PSX_SafeDeleteArray( m_pBuffer );
	}

	//BOOL IndexSource::CheckCanAdd( BOOL bAddingAs16Bit, SIZE_T32 numIndices )
	//{
	//	if ( bAddingAs16Bit && !Is16BitIndices() )
	//	{
	//		PSX_PushError( "Cannot add 16 bit indices Index source uses 32-bit indices." );
	//		return FALSE;
	//	}
	//	else if ( !bAddingAs16Bit && Is16BitIndices() )
	//	{
	//		PSX_PushError( "Cannot add 32-bit indices Index source uses 16-bit indices." );
	//		return FALSE;
	//	}

	//	if ( m_writeMarker + numIndices > m_numIndices )
	//	{
	//		PSX_PushError( "Can't add more indices to Index source stream." );
	//		return FALSE;
	//	}

	//	return TRUE;
	//}
}